<?php

class Application_Model_UserMapper {

    protected $_dbTable;

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_User');
        }
        return $this->_dbTable;
    }

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Ungueltige Gateway-Klasse fuer Tabellendaten');
        }
        $this->_dbTable = $dbTable;
    }

    public function save(Application_Model_UserModel $user) {
        $data = array(
            'id' => $user->getId(),
            'username' => $user->getUsername(),
            'password' => $user->getPassword(),
            'color' => $user->getColor(),
            'salutation' => $user->getSalutation(),
            'gender' => $user->getGender(),
            'email' => $user->getEmail(),
            'confirm' => $user->getConfirm(),
            'active' => $user->getActive(),
        );
        if (null === $user->getId()) {
            unset($data['id']);
            $data['registered'] = time();
            $data['active'] = 0;

            $user->setId($this->getDbTable()->insert($data));
        } else {
            $data['modified'] = time();
            $this->getDbTable()->update($data, array('id = ?' => $data['id']));
        }
        return $this;
    }

    public function find($id, Application_Model_UserModel $user) {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return null;
        }
        $row = $result->current();
        $user->setId($row['id'])
                ->setUsername($row['username'])
                ->setPassword($row['password'])
                ->setColor($row['color'])
                ->setSalutation($row['salutation'])
                ->setGender($row['gender'])
                ->setEmail($row['email'])
                ->setConfirm($row['confirm'])
                ->setActive($row['active']);


        return $user;
    }

    public function delete($userId) {
        if ($userId instanceof Application_Model_UserModel) {
            $userId = $userId->id;
        }
        return $this->getDbTable()->delete(array('id = ?' => $userId));
    }

    public function fetchAll($where = null, $order = null, $count = null, $offset = null) {
        $resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_UserModel();
            $entry->setId($row['id'])
                    ->setUsername($row['username'])
                    ->setSalutation($row['salutation'])
                    ->setEmail($row['email']);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function fetchOne($where = null, $order = null, $count = null, $offset = null) {
        $resultSet = $this->fetchAll($where, $order, $count, $offset);
        return count($resultSet) > 0 ? $resultSet[0] : null;
    }

}

